package com.ecube.maintenance.biz.apis.impl;

import com.ecube.maintenance.biz.apis.IUserAction;
import com.ecube.maintenance.biz.commons.Constants;
import com.ecube.maintenance.biz.commons.ICCallBack;
import com.ecube.maintenance.biz.commons.ICTException;
import com.ecube.maintenance.biz.commons.ITestDataProvider;
import com.ecube.maintenance.biz.commons.VolleySingleton;
import com.ecube.maintenance.pojos.BaseInfo;
import com.ecube.maintenance.pojos.UserInfo;
import com.ecube.maintenance.utils.BaseParameters;
import com.google.gson.reflect.TypeToken;

import java.lang.reflect.Type;

/**
 * Created by Trible Chen on 2014/9/5.
 */
public class UserAction {

    private static ProxyUserAction userAction = new ProxyUserAction();

    /**
     * @param mobile   the mobile number
     * @param callBack invoke the callback after getting result
     */
    public static void fetchValidCodes(String mobile, ICCallBack callBack) {
        userAction.fetchValidCode(mobile, callBack);
    }

    public static void logins(String mobile, String key, ICCallBack<UserInfo> callBack) {
        userAction.login(mobile, key, callBack);
    }

    public static void logouts(String sid, ICCallBack callBack) {
        userAction.logout(sid, callBack);
    }

    public static void updates(UserInfo newInfo, ICCallBack callBack) {
        userAction.update(newInfo, callBack);
    }

    private static class ProxyUserAction implements IUserAction {
        /**
         * @param mobile   the mobile number
         * @param callBack the callBack will be invoke when volley has response
         * @param </>      the class type for the result in callBack,
         *                 represent the BaseInfo's field returnData
         *                 for example,T is UserInfo type when use BaseInfo<UserInfo> as typeToken
         */
        @Override
        public void fetchValidCode(String mobile, ICCallBack callBack) {
            BaseParameters p = new BaseParameters();
            p.add("action", "app_GetLoginKey");
            p.add("mobile", mobile);
            VolleySingleton.getInstance().addToRequestQueue(p, callBack, BaseInfo.class);
        }

        @Override
        public void login(String mobile, String key, final ICCallBack<UserInfo> callBack) {
            BaseParameters p = new BaseParameters();
            p.add("action", "app_Login");
            p.add("key", key);
            p.add("mobile", mobile);
            ICCallBack<UserInfo> callBack1 = new ICCallBack<UserInfo>() {
                @Override
                public void done(UserInfo result, ICTException exception) {
                    if (exception == null) {
                        UserInfo.saveToSpf(result);
                    }
                    if (callBack != null)
                        callBack.done(UserInfo.getInstance(), exception);
                }
            };
            TypeToken<BaseInfo<UserInfo>> typeToken = new TypeToken<BaseInfo<UserInfo>>() {
            };
            Type type = typeToken.getType();
            VolleySingleton.getInstance().addToRequestQueue(p, callBack1, type);
        }

        @Override
        public void logout(String sid, ICCallBack callBack) {
            BaseParameters p = new BaseParameters();
            p.add("action", "app_Login");
            p.add("sid", sid);
            VolleySingleton.getInstance().addToRequestQueue(p, callBack, BaseInfo.class);
        }

        @Override
        public void update(final UserInfo newInfo, final ICCallBack callBack) {
            if (newInfo == null) {
                throw new IllegalArgumentException("newinfo cant be null");
            }
            BaseParameters p = new BaseParameters();
            p.add("action", "app_UpdateCarOwnerInfo");
            p.add("mobile", newInfo.getMobile());
            p.add("sid", newInfo.getSid());
            p.add("carNO", newInfo.getCarNO());
            p.add("realName", newInfo.getRealName());
            p.add("carBrand", newInfo.getCarBrand());
            p.add("carFactory", newInfo.getCarFactory());
            p.add("carSerial", newInfo.getCarSerial());
            p.add("carModel", newInfo.getCarModel());
            ICCallBack<UserInfo> callBack1 = new ICCallBack() {
                @Override
                public void done(Object result, ICTException exception) {
                    if (exception == null) {
                        UserInfo.saveToSpf(newInfo);
                    }
                    if (callBack != null)
                        callBack.done(UserInfo.getInstance(), exception);
                }
            };
            VolleySingleton.getInstance().addToRequestQueue(p, callBack1, BaseInfo.class);
        }

        @Override
        public void fetchUserInfo(String sid, final ICCallBack<UserInfo> callBack) {
            BaseParameters p = new BaseParameters();
            p.add("action", "app_GetCarOwnerInfo");
            p.add("sid", sid);
            ITestDataProvider provider = null;
            if (Constants.LOCAL_TEST_DATA) {
                provider = new ITestDataProvider() {
                    @Override
                    public String getTestData() {
                        return "{\n" +
                                "\"errorCode\": 10000,\n" +
                                "\"errorMsg\": \"\",\n" +
                                "\"returnData\":{\n" +
                                "\"realName\": \"真实姓名\",\n" +
                                "\"mobile\": \"联系手机\",\n" +
                                "\"logo\": \"头像的 url 地址\",\n" +
                                "\"carNO\": \"车牌号码\",\n" +
                                "\"carBrand\": \"车辆品牌\",\n" +
                                "\"carFactory\":\"车厂\",\n" +
                                "\"carSerial\": \"车系\",\n" +
                                "\"carModel\": \"车型\"\n" +
                                "}\n" +
                                "}";
                    }
                };
            }
            ICCallBack<UserInfo> callBack1 = new ICCallBack<UserInfo>() {
                @Override
                public void done(UserInfo result, ICTException exception) {
                    if (exception == null) {
                        UserInfo.saveToSpf(result);
                    }
                    if (callBack != null)
                        callBack.done(UserInfo.getInstance(), exception);
                }
            };
            TypeToken<BaseInfo<UserInfo>> typeToken
                    = new TypeToken<BaseInfo<UserInfo>>() {
            };
            VolleySingleton.getInstance().addToRequestQueue(
                    p, callBack1, typeToken.getType(), provider);

        }
    }
}
